package com.shopnight.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopnight.dto.DelectTakeoutCartsDto;
import com.shopnight.dto.UpdateTakeoutCartDto;
import com.shopnight.entity.TakeoutGoods;
import com.shopnight.entity.TakeoutGoodsSetmeal;
import com.shopnight.entity.TakeoutGoodsSpecificationValue;
import com.shopnight.entity.TakeoutMerchantCart;
import com.shopnight.interceptors.JWTInterceptor;
import com.shopnight.mapper.*;
import com.shopnight.service.TakeoutMerchantCartService;
import com.shopnight.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 13201
* @description 针对表【takeout_merchant_cart】的数据库操作Service实现
* @createDate 2023-03-21 20:55:14
*/
@Service
public class TakeoutMerchantCartServiceImpl extends ServiceImpl<TakeoutMerchantCartMapper, TakeoutMerchantCart>
    implements TakeoutMerchantCartService{

    @Autowired
    TakeoutMerchantCartMapper takeoutMerchantCartMapper;
    @Autowired
    TakeoutGoodsMapper takeoutGoodsMapper;
    @Autowired
    TakeoutGoodsSpecificationValueMapper takeoutGoodsSpecificationValueMapper;

    @Autowired
    TakeoutGoodsSetmealMapper takeoutGoodsSetmealMapper;

    @Override
    public List<TakeoutMerchantCart> getTakeoutMerchantCart(int merchantId) {
        String userId =  UserContext.getEndUserId();
        LambdaQueryWrapper<TakeoutMerchantCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TakeoutMerchantCart::getUserId,userId)
                        .eq(TakeoutMerchantCart::getMerchantId,merchantId);


        List<TakeoutMerchantCart> takeoutMerchantCarts = takeoutMerchantCartMapper.selectList(queryWrapper);

        List<TakeoutMerchantCart> collect = takeoutMerchantCarts.stream().map(takeoutMerchantCart -> {
            Integer specificationValueId = takeoutMerchantCart.getSpecificationValueId();
            if (specificationValueId != null) {
                TakeoutGoodsSpecificationValue takeoutGoodsSpecificationValue = takeoutGoodsSpecificationValueMapper.selectById(specificationValueId);
                takeoutMerchantCart.setPrice(takeoutGoodsSpecificationValue.getPrice());
            } else {
                TakeoutGoods takeoutGoods = takeoutGoodsMapper.selectById(takeoutMerchantCart.getTakeoutGoodsId());
                if (takeoutGoods.getSeckill()!=null && takeoutGoods.getSeckill() == 1) {
                    takeoutMerchantCart.setPrice(takeoutGoods.getPriceSeckil());
                } else takeoutMerchantCart.setPrice(takeoutGoods.getPrice());

            }
            return takeoutMerchantCart;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public void addTakeoutMerchantCart(TakeoutMerchantCart takeoutMerchantCart) {
        //设置用户的id
        String userId =  UserContext.getEndUserId();
        takeoutMerchantCart.setUserId(Integer.valueOf(userId));

        Integer merchantId = takeoutMerchantCart.getMerchantId();
        Integer takeoutGoodsId = takeoutMerchantCart.getTakeoutGoodsId();
        Integer setmealId = takeoutMerchantCart.getSetmealId();
        //判断购物车里面是否有此商品

        LambdaQueryWrapper<TakeoutMerchantCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TakeoutMerchantCart::getUserId,userId)
                .eq(TakeoutMerchantCart::getMerchantId,merchantId)
                .eq(takeoutGoodsId!=null,TakeoutMerchantCart::getTakeoutGoodsId,takeoutGoodsId)
                .eq(setmealId!=null,TakeoutMerchantCart::getSetmealId,setmealId)
                .eq(takeoutMerchantCart.getSpecificationValueId()!=null,TakeoutMerchantCart::getSpecificationValueId,takeoutMerchantCart.getSpecificationValueId())
                .eq(takeoutMerchantCart.getAttribute()!=null,TakeoutMerchantCart::getAttribute,takeoutMerchantCart.getAttribute());

        TakeoutMerchantCart selectOne = takeoutMerchantCartMapper.selectOne(queryWrapper);

        if (selectOne!=null){
            selectOne.setNumber(selectOne.getNumber()+1);
            takeoutMerchantCartMapper.updateById(selectOne);
        }else {

            if ( takeoutGoodsId != null){
                TakeoutGoods takeoutGoods = takeoutGoodsMapper.selectById(takeoutGoodsId);
                takeoutMerchantCart.setName(takeoutGoods.getName());
                takeoutMerchantCart.setImage(takeoutGoods.getImages());
                takeoutMerchantCart.setCategoryId(takeoutGoods.getCategoryId());
                takeoutMerchantCart.setNumber(1);
            }else if( setmealId != null ){
                TakeoutGoodsSetmeal takeoutGoodsSetmeal = takeoutGoodsSetmealMapper.selectById(setmealId);
                takeoutMerchantCart.setName(takeoutGoodsSetmeal.getName());
                takeoutMerchantCart.setImage(takeoutGoodsSetmeal.getImage());
                takeoutMerchantCart.setCategoryId(takeoutGoodsSetmeal.getCategoryId());
                takeoutMerchantCart.setNumber(1);
            }

        //插入数据库
          takeoutMerchantCartMapper.insert(takeoutMerchantCart);
        }

    }

    @Override
    public void deleteTakeoutMerchantCarts(DelectTakeoutCartsDto takeoutCartsDto) {
        LambdaQueryWrapper<TakeoutMerchantCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(takeoutCartsDto.getTakeoutCartIds().size() > 0,TakeoutMerchantCart::getId,takeoutCartsDto.getTakeoutCartIds());
        takeoutMerchantCartMapper.delete(queryWrapper);
    }

    @Override
    public void updateTakeoutMerchantCart(UpdateTakeoutCartDto updateTakeoutCartDto) {
        takeoutMerchantCartMapper.updateTakeoutMerchantCart(updateTakeoutCartDto);
    }


}




